//
//  CaseViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "CaseViewController.h"
#import "DynamicViewTableViewCell.h"
#import "AFNetworking.h"
#import "StyListSonModel.h"
#import "Projects.h"
#import "User.h"
#import "UserImage.h"
#import "DecorateSonViewController.h"
@interface CaseViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)StyListSonModel *sonModel;
@property (nonatomic, strong)NSMutableArray *arr;
@end

@implementation CaseViewController
/**
 *  案例
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self handleData];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64 ) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"DynamicViewTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
  }
- (void)handleData {
    NSString *nameStr = [_urlID stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *footStr = [@"木木" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/projects?start=0&count=10&user=%@", nameStr, footStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:strURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.sonModel = [StyListSonModel modelWithDic:dic];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DecorateSonViewController *vc = [[DecorateSonViewController alloc] init];
    vc.urlId = _sonModel.projects[indexPath.row].Son_id.description;
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sonModel.projects.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DynamicViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.projects = _sonModel.projects[indexPath.row];
    cell.nameImage.layer.cornerRadius = 35;
        return cell;
    }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
